using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace ScheduleAlertJob
{
    /// <summary>
    /// ScheduleAlertJobInstaller class, inherit from SPFeatureReceiver.
    /// </summary>
    public class ScheduleAlertJobInstaller : SPFeatureReceiver 
    {
        /// <summary>
        /// Overrides FeatureActivated
        /// </summary>
        /// <param name="properties"><see cref="SPFeatureReceiverProperties"/></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
            SPSite site = properties.Feature.Parent as SPSite;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == Consts.JobTitle)
                    job.Delete();
            }

            //PermissionMonitorJob permMonitorJob = new PermissionMonitorJob(Const.JOBTITLE, site.WebApplication);
            ScheduleAlertJobDefinition scheduleAlertJob = new ScheduleAlertJobDefinition(Consts.JobTitle,
                site.WebApplication, null, SPJobLockType.None);
                            

            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = Consts.Interval; ;

            scheduleAlertJob.Schedule = schedule;

            scheduleAlertJob.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
            SPSite site = properties.Feature.Parent as SPSite;

            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == Consts.JobTitle)
                    job.Delete();
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }
    }
}
